<template>
	<view class="paintingTool" :class="theme + '-bg-middle'">
		<painting-tools cid="canvas" @sumbit="sumbit" @fail="fail" />
	</view>
</template>

<script>
	import PaintingTools from './components/PaintingTools.vue'
	import {
		base64ToPath,
		pathToBase64
	} from '../../../components/mmmm-image-tools/index.js'
	import permision from "@/components/wa-permission/permission.js"
	export default {
		components: {
			PaintingTools
		},
		data() {
			return {
				src2: '',
			};
		},
		onLoad() {
			this.$theme()
		},
		methods: {
			sumbit(res) {
				this.url = res.tempFilePath
				pathToBase64(res.tempFilePath).then(base64 => {
					this.src2 = base64

					this.savePoster()
				}).catch(error => {
					console.log(error)

				})
			},
			fail(err) {
				console.log('fail', err)
			},
			async requestAndroidPermission(permisionID) {
				var result = await permision.requestAndroidPermission(permisionID)
				var strStatus
				if (result == 1) {
					strStatus = "已获得授权"
					console.log(666)
					this.saveImageToPhotosAlbum();
				} else if (result == 0) {
					strStatus = "未获得授权"
				} else {
					uni.showToast({
						title: "保存失败，请打开权限功能重试",
						icon: "none"
					});
				}
			},
			savePoster() {

				var isIos
				// #ifdef APP-PLUS
				isIos = (plus.os.name == "iOS")
				if (isIos) {
					this.saveImageToPhotosAlbum();
				} else {
					console.log(666)
					// this.saveImageToPhotosAlbum();
					this.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE')
				}
				// #endif

				// #ifdef MP-WEIXIN
				uni.getSetting({ //获取用户的当前设置
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
							this.saveImageToPhotosAlbum();
						} else {
							uni.authorize({ //如果没有授权，向用户发起请求
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.saveImageToPhotosAlbum();
								},
								fail: () => {
									uni.showToast({
										title: "保存失败，请打开保存相册权限",
										icon: "none",
										duration: 3000
									});
									uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
										success: (res2) => {}
									});
								}
							})
						}
					}
				})
				// #endif
			},
			saveImageToPhotosAlbum() {
				uni.showLoading({
					title: '保存中...',
					mask: true
				})
				// #ifdef APP-PLUS
				console.log(this.url)
				uni.saveImageToPhotosAlbum({
					filePath: this.url,
					success: function() {
						uni.showToast({
							title: '图片保存成功',
							icon: 'none',
							duration: 2200
						});
					},
					fail: function(fail) {
						console.log(fail)
						uni.showModal({
							content: '检测到您没打开获取信息功能权限，是否去设置打开？',
							confirmText: "确认",
							cancelText: '取消',
							success: (res2) => {
								if (res2.confirm) {
									permision.gotoAppPermissionSetting()
								} else {
									uni.showToast({
										title: "保存失败，请打开权限功能重试",
										icon: "none"
									});
								}
							}
						})
					}
				});
				// uni.downloadFile({
				// 	url: this.result,
				// 	success: function(res1) {
				// 		console.log(res1)
				// 		if (res1.statusCode) {


				// 		}
				// 	},
				// 	fail: function() {
				// 		console.log('fail')
				// 	}
				// })
				// #endif
				// #ifdef MP-WEIXIN
				let base64 = this.src2.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
				let filePath = wx.env.USER_DATA_PATH + '/ph_fit_qrcode.png';
				console.log(filePath)
				uni.getFileSystemManager().writeFile({
					filePath: filePath, //创建一个临时文件名
					data: base64, //写入的文本或二进制数据
					encoding: 'base64', //写入当前文件的字符编码
					success: res => {
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: function(res2) {
								uni.hideLoading();
								uni.showToast({
									title: '保存成功',
									icon: "none",
									duration: 1000
								})
							},
							fail: function(err) {
								uni.hideLoading();
								// console.log(err.errMsg);
							}
						})
					},
					fail: err => {
						uni.hideLoading();
						//console.log(err)
					}
				})
				// #endif
			},
		}
	}
</script>

<style lang="scss">
	.paintingTool {
		height: 100%;
	}
</style>
